package org.gwt.grin.rebind.features;

import org.gwt.grin.client.features.Text;
import org.gwt.grin.client.util.BgImage;
import org.gwt.grin.client.util.Color;
import org.gwt.grin.client.util.FontSpec;
import org.gwt.grin.rebind.ShowBuilder;

import com.google.gwt.uibinder.rebind.IndentedWriter;

public class SEText extends SEBox {

	public static final int DEFAULT_LINE_HEIGHT = 100;

	public static final int TEXT_ALIGN_DEFAULT = 0;
	public static final int TEXT_ALIGN_LEFT = 1;
	public static final int TEXT_ALIGN_RIGHT = 2;
	public static final int TEXT_ALIGN_CENTER = 3;

	private final String text;
	private final boolean wordWrap;
	private final FontSpec fontSpec;
	private final int textAlign;
	private final int lineHeight;

	public SEText(String name, int x, int y, int width, int height,
			String text, boolean wordWrap, FontSpec fontSpec, int outlineWidth,
			Color outlineColor, Color fillColor, BgImage bgImage,
			int textAlign, int lineHeight) {
		super(name, x, y, width, height, outlineWidth, outlineColor, fillColor,
				bgImage);
		this.text = text;
		this.wordWrap = wordWrap;
		this.fontSpec = fontSpec;
		this.textAlign = textAlign;
		this.lineHeight = lineHeight;
	}

	@Override
	protected String getClassName() {
		return Text.class.getName();
	}

	@Override
	public void setup(IndentedWriter w, ShowBuilder builder) {
		String vn = getVN(builder);
		w.write("%s.setup(%d, %d, %d, %d, %d, %s, %s, %s, %s);", vn, x, y,
				width, height, outlineWidth, makeColor(outlineColor),
				makeColor(fillColor), makeBgImage(bgImage),
				makeScalingModel(builder));
		w.write("%s.setText(\"%s\");", vn, text);
		w.write("%s.setWordWrap(%b);", vn, wordWrap);
		w.write("%s.setFontSpec(%s);", vn, makeFontSpec(fontSpec));
		w.write("%s.setLineHeight(%d);", vn, lineHeight);
		switch (textAlign) {
		case TEXT_ALIGN_LEFT:
			w.write("%s.setHorizontalAlignment(%s);", vn,
					"HasAlignment.ALIGN_LEFT");
			break;
		case TEXT_ALIGN_RIGHT:
			w.write("%s.setHorizontalAlignment(%s);", vn,
					"HasAlignment.ALIGN_RIGHT");
			break;
		case TEXT_ALIGN_CENTER:
			w.write("%s.setHorizontalAlignment(%s);", vn,
					"HasAlignment.ALIGN_CENTER");
			break;
		default:
			w.write("%s.setHorizontalAlignment(%s);", vn,
					"HasAlignment.ALIGN_DEFAULT");
			break;
		}
	}

}
